package com.why.serviceedu.controller;


import com.why.commonresults.Result;
import com.why.servicebase.exception.GuilException;
import com.why.serviceedu.client.VodClient;
import com.why.serviceedu.bean.EduVideo;

import com.why.serviceedu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author why
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService service;
    @Autowired
    VodClient vodClient;

    /**
     * 添加小节
     */
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody EduVideo videoVo) {
        System.out.println(videoVo);
        service.save(videoVo);
        return Result.ok();

    }


    /**
     * todo 修改小节
     */

//    @PostMapping("/updateVideo")
//    public Result  updateVideo(@RequestBody VideoVo videoVo){
//        service.updateVideoVo(videoVo);
//        return Result.ok();
//
//    }

    @GetMapping("/deleteVideo/{id}")
    public Result deleteVideo(@PathVariable String id) {
        //先根据小节id获取视频id
        EduVideo videoDelete = service.getById(id);
        //根据小节的id删除阿里云的视频
        String videoSourceId = videoDelete.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            Result result = vodClient.deleteVideo(videoDelete.getVideoSourceId());
            if (result.getCode() == 20001) {
                throw new GuilException(20001, "删除视频失败！熔断器启动");
            }

        }


        //删除完视频后删除小节
        service.removeById(id);
        return Result.ok();

    }

}

